home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / smoothParamSurface.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  5.9 KB  |  198 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. // smoothParamSurface - reparameterize a surface to get smoother parameter
  18. // distribution in both directions
  19. // 
  20. // Usage: 
  21. //   smoothParamSurface(int <history>, int <nsamples>); 
  22. //
  23. //  where: <history> is a toggle turning history on or off
  24. //         <nsamples> is the number of output patches per input patch
  25. //                    in the U direction.
  26. // 
  27. // How it works:
  28. //   1. Extracts isoparms in V direction, rebuilds as uniform
  29. //   2. Lofts these splines in U using chord length option
  30. // 
  31. // Usage hints:
  32. //  - Using a larger number of samples increases accuracy, but can get
  33. //      slow. Try with 1 first, then increase as needed.
  34. //  - history on means the original surface can be tweaked and the changes
  35. //    will be seen on the reparameterized surface, but the cost is the storage
  36. //    of a large number of intermediate curves used to generate the reparameterized
  37. //    surface.
  38. //
  39. // Author: MPW
  40. //
  41. //
  42. proc int getNurbsSurfaceKnots(string $srfName, float $uKnots[], float $vKnots[] )
  43. //
  44. //    Description :
  45. //    Get surface knots in U and V
  46. {
  47.  
  48.     // create info Node.
  49.     string $infoNode ;
  50.     if( catch( $infoNode = `createNode surfaceInfo` ) ) {
  51.         return 1; // failed
  52.     } 
  53.  
  54.     // connect surface on to the info node.
  55.     string $outAttr = $srfName + ".local" ; 
  56.     string $inAttr = $infoNode + ".is" ;
  57.     connectAttr $outAttr $inAttr ;
  58.  
  59.     // read the knots.
  60.     $uKnots = `getAttr ($infoNode + ".knotsU")`;    
  61.     $vKnots = `getAttr ($infoNode + ".knotsV")`;    
  62.  
  63.     // delete surface info node.
  64.     delete $infoNode ;
  65.  
  66.     // worked
  67.     return 0;
  68. }
  69.  
  70. proc int getNurbsSurfaceSmoothParam( 
  71.     string $srf, int $ch, int $samplesPerSpan)
  72. //
  73. //    Description :
  74. //    Compute reparam surface by computing points 
  75. //  regularly spaced in U and V on the surface, fitting curves
  76. //  and then lofting a surface through the curves.
  77. // 
  78. {
  79.     int $i; // loop counter
  80.  
  81.     // number of spans
  82.     int $nspansU = eval("getAttr " + $srf + ".spansU");
  83.  
  84.     // degree
  85.     int $degreeU = eval("getAttr " + $srf + ".degreeU");
  86.  
  87.     // knots
  88.     float $uKnots[];
  89.     float $vKnots[];
  90.     if(getNurbsSurfaceKnots($srf, $uKnots, $vKnots)) {
  91.         return 1; // failed
  92.     }
  93.  
  94.     // first and last knot values to use 
  95.     int $firstU = $degreeU - 1;
  96.     int $lastU = $nspansU + $firstU;
  97.  
  98.     // loop over knot values
  99.     int $uIndex, $vIndex;
  100.     int $uSample, $vSample;
  101.     int $uSampleMax, $vSampleMax;
  102.     float $u;
  103.  
  104.     string $allCurves;
  105.  
  106.     // check if periodic in U. If so, dont need to evaluate last U
  107.     // isoparm on surface since it is coincident with the first
  108.     int $formInU = eval("getAttr " + $srf + ".formU");
  109.  
  110.     // loop over U spans
  111.     for($uIndex = $firstU; $uIndex < $lastU; $uIndex++) {
  112.  
  113.         // maximum number of samples for this span
  114.         $uSampleMax = ($formInU == 2 || $uIndex < $lastU-1) ? $samplesPerSpan - 1 : $samplesPerSpan;
  115.  
  116.         // parameter interval for this span
  117.         float $uInterval = $uKnots[$uIndex+1] - $uKnots[$uIndex];
  118.  
  119.         // loop over U samples
  120.         for($uSample = 0; $uSample <= $uSampleMax; $uSample++) {
  121.  
  122.             // get value of U
  123.             $u = $uKnots[$uIndex] + $uSample*$uInterval/float($samplesPerSpan);
  124.             // make sure rouding errors dont take it off the end
  125.             if($uIndex == ($formInU == 2 || $lastU-1) && $uSample == $uSampleMax) {
  126.                 $u = $uKnots[$lastU];
  127.             }
  128.  
  129.             // extract this isoparm
  130.             string $dup[] = eval("duplicateCurve -ch " + $ch + " -rn 0 -local 0 " + $srf + ".u[" + $u + "]");
  131.             int $nspans = eval("getAttr " + $dup[0] + ".spans");
  132.             // rebuild isoparm with uniform knots - replace original extracted isoparm
  133.             string $reb[] = eval("rebuildCurve -ch " + $ch + " -rpo 1 -rt 0 -kr 1 -kcp 0 -kep 1 -kt 0 -s " + $nspans + " -d 3 -tol 0.05 " + $dup[0]);
  134.             
  135.             $allCurves += " " + $reb[0];
  136.         }
  137.     }
  138.     //print ("All curves are: " + $allCurves + "\n");
  139.  
  140.     // now loft the curves using reparam option
  141.     string $loftCmd = "loft -ch " + $ch + " -d 3 -u 0";
  142.     if($formInU == 2) $loftCmd += " -c on ";
  143.     $loftCmd += $allCurves;
  144.     //print("Loft command is " + $loftCmd + "\n");
  145.  
  146.     // close flag - check basic surface first...
  147.      string $outsrf[] = eval($loftCmd);
  148.      print ("Created reparam surface " + $outsrf[0] + "\n");
  149.  
  150.      // delete all input curves if no history requested
  151.        if(0 == $ch) {
  152.         eval("delete " + $allCurves);
  153.     }
  154.  
  155.     // return the surface name??
  156.     return 0;
  157. }
  158.  
  159.  
  160. global proc smoothParamSurface(int $ch, int $samplesPerSpan)
  161. {
  162.  
  163.     if($ch>=1) {
  164.         print "Setting construction history on\n";
  165.         $ch = 1;
  166.     }
  167.  
  168.     if($ch<=0) {
  169.         print "Setting construction history off\n";
  170.         $ch = 0;
  171.     }
  172.  
  173.     // always need at least one sample per span
  174.     if($samplesPerSpan < 1) $samplesPerSpan = 1;
  175.  
  176.     // Run filter to select only the NURBS surfaces
  177.     global int $gSelectNurbsSurfacesBit ;
  178.     string $srfList[] = `filterExpand -ex true 
  179.         -sm $gSelectNurbsSurfacesBit`;
  180.     int $len = size($srfList) ;
  181.     if( $len == 0 ) {
  182.         print "No NURBS surfaces selected\n" ;
  183.         return;
  184.     }
  185.  
  186.     // Work on all surfaces
  187.     for($srfNum = 0; $srfNum < $len; $srfNum++) {
  188.         string $srf = $srfList[$srfNum] ;
  189.  
  190.         // do the smoothParam
  191.         if(getNurbsSurfaceSmoothParam($srf, $ch, $samplesPerSpan)) {
  192.             print ("Failed to reparameterize surface " + $srf + "\n");
  193.             break;
  194.         }
  195.     }
  196.  
  197. }
  198.